﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPF基础.项目
{
    /// <summary>
    /// 移动方块.xaml 的交互逻辑
    /// </summary>
    public partial class 移动方块 : Window
    {
        public 移动方块()
        {
            InitializeComponent();
        }

        private void Grid_KeyDown(object sender, KeyEventArgs e)
        {
            //判断当前选中位置
            UIElementCollection Children = Grid_1.Children;
            Border Current_Border = new Border();

            //背景颜色全部设成前景色
            for (int i = 0; i < Children.Count; i++)
            {               
                if(Children[i] is Border && ((Children[i] as Border).Background as SolidColorBrush).Color.Equals(Colors.Transparent))
                {
                    Current_Border = Children[i] as Border;
                    (Children[i] as Border).Background = new SolidColorBrush(Colors.BlueViolet);
                }
            }
            string Border_Name = Current_Border.Name;
            int Border_Index = Convert.ToInt16(Border_Name.Replace("B", ""));
            //判断按键方位
            if (e.Key.Equals(Key.Up))
            {
                Border_Index = Border_Index - 3 <= 9 ? Border_Index - 3 : Border_Index + 5;
            }
            else if(e.Key.Equals(Key.Down))
            {
                Border_Index = Border_Index + 3 <= 9 ? Border_Index + 3 : Border_Index - 5;

            }
            else if(e.Key.Equals(Key.Left))
            {
                Border_Index = Border_Index - 1 <= 0 ? Border_Index -1 : Border_Index +8;

            }
            else if(e.Key.Equals(Key.Right))
            {
                Border_Index = Border_Index + 1 <= 9 ? Border_Index + 1 : Border_Index - 8;

            }
            object Control=Grid_1.FindName("B" + Border_Index);
            if(Control!=null)
            {
                (Control as Border).Background = new SolidColorBrush(Colors.Transparent);
            }


        }
    }
}
